home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 284 < prev    next >
Text File  |  1996-08-06  |  3KB  |  81 lines

  1. Newsgroups: comp.std.c
  2. Path: news.netins.net!duke!scottm
  3. From: scottm@uis.com (Scott Miller)
  4. Subject: Re: HELP!! Beginner's question...
  5. Message-ID: <DM5LC6.M2n@uis.com>
  6. Date: Fri, 2 Feb 1996 14:52:53 GMT
  7. References: <Pine.OSF.3.91l.960131005708.7552A-100000@saul3.u.washington.edu>
  8. Organization: Unix Integration Services
  9.  
  10.  
  11. I compiled your program and ran it on our HP 700 series Unix machine,
  12. entered 5 as the non-negative integer and 7 as the power, and got 78,125.
  13. The program appears to be correct.
  14.  
  15. This could be a system dependant problem -- different systems may use
  16. different sizes for int and long int.  On  your system, int may be a 
  17. 16 bit number in which case it would not be large enough to store 78,125.
  18. (2^16 = 65536, but remember he's using signed numbers so Max int is 
  19. smaller yet).  Your instructor's advice to use long int sounds wise --
  20. try it if you haven't already.
  21.  
  22.                 Scott Miller
  23.  
  24.  
  25.  
  26. Ramon Mariano Jr <rmariano@u.washington.edu> writes:
  27. >Hello everybody!
  28.  
  29. >I'm a beginning C-programmer and for the past couple of days, I've been
  30. >totally stuck in my program. The purpose of my program is to ask the user
  31. >for an integer 'i' and raise it by a power 'n'. I've been succesful in
  32. >computing 5 to the 6th power, 3 to the 4th power, and other small-sized
  33. >equations. However, when I try to raise 5 to the 7th power, I get "12589" 
  34. >when it really should be "78125". My teacher told me that instead of using
  35. >plain 'int', I should use 'long int'. I tried that, but it's still not
  36. >working. If anyone can help me out here, I'd GREATLY appreciate it! I've
  37. >included my code below... (by the way, I'm not allowed to use the <math.h>
  38. >library)
  39.  
  40.  
  41. >------------------------------------------------------------------------
  42.  
  43. >#include <stdio.h>
  44. >                  
  45. >int main(void)
  46. >{
  47. >   int i,         /* integer */
  48. >       n,         /* power to raise integer */
  49. >       i_total,   /* integer's total value*/
  50. >       count;     /* keeps track of number of loops run */
  51.  
  52. >   /* asks for an integer and stores it in 'i' */
  53. >   printf("Enter a non-negative integer: ");
  54. >   scanf("%d", &i); 
  55.  
  56. >   /* asks for a power and stores it in 'n' */
  57. >   printf("What power should we raise it to? ");
  58. >   scanf("%d", &n);
  59.  
  60. >   /* if 'n' is negative, program will treat it as a '0' */    
  61. >   if (n < 0){
  62. >       n = 0;
  63. >   }            
  64. >                    
  65. >    i_total = 1;       /* gives i_total an initial total of '1' */
  66.  
  67. >    for (count = 1;  count <= n;  count = count + 1){
  68. >        i_total = i_total * i;
  69. >    }
  70.  
  71. >    /* prints out the inputs and result */
  72. >    printf("%d raised to the %dth power is %d\n\n", i, n, i_total); 
  73.  
  74. >    return(0);    
  75. >}
  76. -- 
  77. --
  78. +-----------------------------------------------------------------+
  79. | Scott A. Miller                                                 |
  80. | Unix Integration Services                        scottm@uis.com |
  81.